package com.srit.recipes;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.http.MediaType;
import org.springframework.scheduling.annotation.EnableScheduling;
import springfox.documentation.oas.annotations.EnableOpenApi;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
@EnableOpenApi
@EnableJpaAuditing
@EnableAspectJAutoProxy
@EnableScheduling
public class RecipesApplication {

    public static void main(String[] args) {
        SpringApplication.run(RecipesApplication.class, args);
    }


    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
        // 1.定义一个converters转换消息的对象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
        List<MediaType> mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.APPLICATION_JSON);
        mediaTypes.add(MediaType.ALL);
        fastConverter.setSupportedMediaTypes(mediaTypes);
        // 2.添加fastjson的配置信息，比如: 是否需要格式化返回的json数据
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setCharset(StandardCharsets.UTF_8);
        fastJsonConfig.setSerializerFeatures(
                //是否输出null字段
                SerializerFeature.WriteMapNullValue,
                //集合输出[]而非null
                SerializerFeature.WriteNullListAsEmpty,
                //字符串输出""而非null
                SerializerFeature.WriteNullStringAsEmpty,
                //关闭重复引用
                SerializerFeature.DisableCircularReferenceDetect,
                //数字输出为0
                SerializerFeature.WriteNullNumberAsZero,
                //boolean 输出为false
                SerializerFeature.WriteNullBooleanAsFalse,
//                SerializerFeature.WriteNonStringValueAsString,
                SerializerFeature.WriteEnumUsingToString,
                SerializerFeature.PrettyFormat
        );
        // 3.在converter中添加配置信息
        fastConverter.setFastJsonConfig(fastJsonConfig);
        return fastConverter;
    }


}
